package com.rfid.service;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.rfid.client.SettingWindow;
import com.rfid.service.exception.ClientException;
import com.teatrace.model.Product;

public class RFIDSaveInfoService {
	
	private SettingWindow settingWindow;
	private static final Logger log = Logger.getLogger(RFIDSaveInfoService.class);
	private static Product product = null;
	public RFIDSaveInfoService() {
	}
	
	public RFIDSaveInfoService(SettingWindow settingWindow) {
		this.settingWindow = settingWindow;
	}
	
	public void saveInObject() {
		product = new Product();
		String id = settingWindow.getIdText().getText();
		String name = settingWindow.getNameText().getText();
		product.setProductNum(id);
		product.setName(name);
		Properties prop = new Properties();
		
		String path = this.getClass().getClassLoader().getResource("").getPath();
		prop.setProperty("ID", id);
		prop.setProperty("NAME", name);
		FileOutputStream out;
		try {
			out = new FileOutputStream(new File(path+"initAttribute.properties"));
			prop.store(out,"");
		} catch (Exception e) {
			log.error(e);
			throw new ClientException(e);
		} 
	}

	public static Product getProduct() {
		return product;
	}
	public static void setProduct(Product product) {
		RFIDSaveInfoService.product = product;
	}
}
